<?php
class singlepageService extends commonService{
    
    public function __construct(){
        parent::__construct();
        $this->include_viewer_dir=P_VIEWER.'singlepage/';
		$this->def_temp='index.php';
    }
    public function t(){
        return array('singlepage');
    }
    public function my_template($selected=''){
        $dir=$this->include_viewer_dir;
        $t=template_files($dir);
        unset($t[$this->def_temp]);
        $default="<option value=''>默认模板[{$this->def_temp}]</option>\r\n";
        return $default.form_select($t,$selected);
    }

    //文件写入
	private function _put($file,$data){
		$dir=dirname($file);
		if(!is_dir($dir)) mkdir($dir,0777,true);
		return file_put_contents($file,$data);
	}
    //批量更新arc html
	public function make_html($items){
		if(!is_array($items)) return false;
		if(!count($items)) return false;
		foreach($items as $k=>$v) $this->_html($v);
		return true;
	}
    //生成单页
	private function _html($id){
		
		//查询文章信息
		$query="select * from `{$this->t[0]}` where `id`=:id limit 1";
		$item=$this->m->select($query,array('id'=>$id));
		if(!is_array($item)) return false;

		$http='';
        if($item['htmlurl']){
            $file=ROOT.ltrim($item['htmlurl'],'/');
        }else{
            return true;
        }
		
		ob_start();
		include template($this->include_viewer_dir,$item['template'],$this->def_temp);
		$data = ob_get_contents();
		ob_clean();
		$this->_put($file,$data);
		return true;
	}
    //编辑 删除时用
	private function unlink_html($items){
		if(!is_array($items)) return false;
		if(!count($items)) return false;
		foreach($items as $k=>$v){
			$query="select `id`,`htmlurl` from `{$this->t[0]}` where `id`=:id limit 1";
			$r=$this->m->select($query,array('id'=>$v));
			if(!$r['id']) continue;
			$http=$r['htmlurl'];
			$file=ROOT.trim($http,'/');
			if($http && file_exists($file)) unlink($file);
		}
		return true;
	}
    private function set_htmlurl($htmlurl,$id){
        if(!$htmlurl){
            $htmlurl='/about/'.$id.'.html';
            $query="update `{$this->t[0]}` set `htmlurl`='{$htmlurl}' where `id`='{$id}' limit 1";
            $this->m->query($query);
        }
    }
    public function add_($post){
		
        $result=array();
        
        $vali=validate::check('singlepage',$post,'add');
        if($vali){
            $result[0]=$this->model->_i($this->t[0],$post);
            if($result[0]){
                $insert_id=$this->m->insert_id();
                $this->set_htmlurl($post['htmlurl'],$insert_id);
                $this->make_html(array($insert_id));
            }
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
    }
    public function mod($id){
        $data=$this->model->_f($this->t[0],'*',$id);
		return $data;
    }
	
    public function mod_($post,$id){
        $result=array();
        
        $vali=validate::check('singlepage',$post,'mod',array('id::!='=>$id));
        if($vali){
    		$result[0]=$this->model->_u($this->t[0],$post,$id);
            if($result[0]){
                $this->set_htmlurl($post['htmlurl'],$id);
                $this->make_html(array($id));
            }
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
    public function del($id){
        $return=array();
        $this->unlink_html(array($id));//删html
		$return[0]=$this->model->_d($this->t[0],$id);
        return $return;
    }
    public function li($condition=array(),$extra='',$extra_data=array()){
        $return=array();
        $query="select * from `{$this->t[0]}` where 1 {$extra} order by `id` desc";
        $p=new smart_pager(array($query,$extra_data),$this->m);
        //$p->address('&block_id=%block_id%','block_id',$condition['block_id']);
		foreach($condition as $k=>$v){
			$p->address("&{$k}=%{$k}%",$k,$v);
		}
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query,$extra_data);
            $return[]=$p;
        }
        return $return;
    }
//
}